package main.singleton;

/**
 * @Author: SerMs
 * @Date: 2024/06/17 16 58
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 **/
public class Singleton02 {
    private static final Singleton02 INSTANCE;

    // 使用静态代码块去初始化
    static {
        INSTANCE = new Singleton02();
    }

    private Singleton02() {
    }

    public static Singleton02 getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            // 多线程
            new Thread(() ->
                    System.out.println(INSTANCE.hashCode())
            ).start();
        }

        System.out.println("=====");
        Singleton02 demo01 = Singleton02.getInstance();
        Singleton02 demo02 = Singleton02.getInstance();
        System.out.println(demo02.equals(demo01));
        System.out.println(demo02 == demo01);
    }

}
